<html>
<head>
<meta http-equiv="Content-Type" content="text/html; charset=UTF-8">
<title>Introspecting function template</title>
<link rel="stylesheet" href="../../../../../doc/src/boostbook.css" type="text/css">
<meta name="generator" content="DocBook XSL Stylesheets V1.79.1">
<link rel="home" href="../index.html" title="Chapter 1. The Type Traits Introspection Library">
<link rel="up" href="../index.html" title="Chapter 1. The Type Traits Introspection Library">
<link rel="prev" href="tti_detail_has_function.html" title="Introspecting function">
<link rel="next" href="tti_nested_type.html" title="Nested Types">
</head>
<body bgcolor="white" text="black" link="#0000FF" vlink="#840084" alink="#0000FF">
<table cellpadding="2" width="100%"><tr>
<td valign="top"><img alt="Boost C++ Libraries" width="277" height="86" src="../../../../../boost.png"></td>
<td align="center"><a href="../../../../../index.html">Home</a></td>
<td align="center"><a href="../../../../../libs/libraries.htm">Libraries</a></td>
<td align="center"><a href="http://www.boost.org/users/people.html">People</a></td>
<td align="center"><a href="http://www.boost.org/users/faq.html">FAQ</a></td>
<td align="center"><a href="../../../../../more/index.htm">More</a></td>
</tr></table>
<hr>
<div class="spirit-nav">
<a accesskey="p" href="tti_detail_has_function.html"><img src="../../../../../doc/src/images/prev.png" alt="Prev"></a><a accesskey="u" href="../index.html"><img src="../../../../../doc/src/images/up.png" alt="Up"></a><a accesskey="h" href="../index.html"><img src="../../../../../doc/src/images/home.png" alt="Home"></a><a accesskey="n" href="tti_nested_type.html"><img src="../../../../../doc/src/images/next.png" alt="Next"></a>
</div>
<div class="section">
<div class="titlepage"><div><div><h2 class="title" style="clear: both">
<a name="the_type_traits_introspection_library.tti_detail_has_function_template"></a><a class="link" href="tti_detail_has_function_template.html" title="Introspecting function template">Introspecting
    function template</a>
</h2></div></div></div>
<p>
      We can introspect a member function template or a static member function template
      of a user-defined type using the TTI functionality we shall now explain.
    </p>
<p>
      A function template of a user-defined type can either be a member function
      template or a static member function template. An example would be:
    </p>
<pre class="programlisting"><span class="keyword">struct</span> <span class="identifier">AType</span>
  <span class="special">{</span>
  <span class="keyword">template</span><span class="special">&lt;</span><span class="keyword">class</span> <span class="identifier">X</span><span class="special">,</span><span class="keyword">class</span> <span class="identifier">Y</span><span class="special">,</span><span class="keyword">class</span> <span class="identifier">Z</span><span class="special">&gt;</span> <span class="keyword">double</span> <span class="identifier">AFuncTemplate</span><span class="special">(</span><span class="identifier">X</span> <span class="identifier">x</span><span class="special">,</span><span class="identifier">Y</span> <span class="special">*</span> <span class="identifier">y</span><span class="special">,</span><span class="identifier">Z</span> <span class="special">&amp;</span> <span class="identifier">z</span><span class="special">)</span>
    <span class="special">{</span> <span class="special">...</span><span class="identifier">some</span> <span class="identifier">code</span> <span class="keyword">using</span> <span class="identifier">x</span><span class="special">,</span><span class="identifier">y</span><span class="special">,</span><span class="identifier">z</span><span class="special">;</span> <span class="keyword">return</span> <span class="number">0.0</span><span class="special">;</span> <span class="special">}</span>
  <span class="keyword">template</span><span class="special">&lt;</span><span class="keyword">class</span> <span class="identifier">X</span><span class="special">,</span><span class="keyword">int</span> <span class="identifier">Y</span><span class="special">&gt;</span> <span class="keyword">static</span> <span class="keyword">int</span> <span class="identifier">AFuncTemplate</span><span class="special">(</span><span class="identifier">X</span> <span class="identifier">x</span><span class="special">)</span>
    <span class="special">{</span> <span class="special">...</span><span class="identifier">some</span> <span class="identifier">code</span> <span class="keyword">using</span> <span class="identifier">x</span><span class="special">;</span> <span class="keyword">return</span> <span class="identifier">Y</span><span class="special">;</span> <span class="special">}</span>
  <span class="special">};</span>
</pre>
<p>
      A function template <code class="computeroutput"><span class="identifier">AFuncTemplate</span></code>
      is a member function template of the <code class="computeroutput"><span class="identifier">AType</span></code>
      user-defined type and a different function template also called <code class="computeroutput"><span class="identifier">AFuncTemplate</span></code> is a static member function
      template of the <code class="computeroutput"><span class="identifier">AType</span></code> user-defined
      type.
    </p>
<p>
      In order to introspect either function template we use some theoretical valid
      instantiations of <code class="computeroutput"><span class="identifier">AFuncTemplate</span></code>.
      An instantiation of a function template was previously explained in the topic
      <a class="link" href="tti_function_templates.html#sectti_function_templates">"Introspecting function templates
      technique"</a>.
    </p>
<p>
      For the purposes of illustration the instantiation we will use is:
    </p>
<pre class="programlisting"><span class="keyword">double</span> <span class="identifier">AFuncTemplate</span><span class="special">&lt;</span><span class="keyword">char</span><span class="special">,</span><span class="keyword">bool</span><span class="special">,</span><span class="keyword">int</span><span class="special">&gt;(</span><span class="keyword">char</span><span class="special">,</span><span class="keyword">bool</span> <span class="special">*,</span><span class="keyword">int</span> <span class="special">&amp;)</span>
</pre>
<p>
      What we have now which the TTI will need in order to introspect the function
      template <code class="computeroutput"><span class="keyword">template</span><span class="special">&lt;</span><span class="keyword">class</span> <span class="identifier">X</span><span class="special">,</span><span class="keyword">class</span> <span class="identifier">Y</span><span class="special">,</span><span class="keyword">class</span> <span class="identifier">Z</span><span class="special">&gt;</span>
      <span class="keyword">double</span> <span class="identifier">AFuncTemplate</span><span class="special">(</span><span class="identifier">X</span><span class="special">,</span><span class="identifier">Y</span> <span class="special">*,</span><span class="identifier">Z</span>
      <span class="special">&amp;)</span></code> within the <code class="computeroutput"><span class="identifier">AType</span></code>
      struct is:
    </p>
<div class="itemizedlist"><ul class="itemizedlist" style="list-style-type: disc; ">
<li class="listitem">
          The name of <code class="computeroutput"><span class="identifier">AFuncTemplate</span></code>
        </li>
<li class="listitem">
          The template parameters of <code class="computeroutput"><span class="keyword">char</span><span class="special">,</span><span class="keyword">bool</span><span class="special">,</span><span class="keyword">int</span></code>
        </li>
<li class="listitem">
          The enclosing type of <code class="computeroutput"><span class="identifier">AType</span></code>
        </li>
<li class="listitem">
          The return type of <code class="computeroutput"><span class="keyword">double</span></code>
        </li>
<li class="listitem">
          The function parameters of <code class="computeroutput"><span class="keyword">char</span><span class="special">,</span><span class="keyword">bool</span> <span class="special">*,</span><span class="keyword">int</span> <span class="special">&amp;</span></code>
        </li>
</ul></div>
<h4>
<a name="the_type_traits_introspection_library.tti_detail_has_function_template.h0"></a>
      <span class="phrase"><a name="the_type_traits_introspection_library.tti_detail_has_function_template.generating_the_metafunction"></a></span><a class="link" href="tti_detail_has_function_template.html#the_type_traits_introspection_library.tti_detail_has_function_template.generating_the_metafunction">Generating
      the metafunction</a>
    </h4>
<p>
      As with all TTI functionality for introspecting entities within a user-defined
      type introspecting a function template is a two step process. The first process
      is using a macro to generate a metafunction. The macro for function templates
      is <code class="computeroutput"><a class="link" href="../BOOST_TTI_HAS_FUNC_idm6162.html" title="Macro BOOST_TTI_HAS_FUNCTION_TEMPLATE">BOOST_TTI_HAS_FUNCTION_TEMPLATE</a></code>.
      This macro takes the name of the member function template and the instantiated
      template parameters, the first two items in our list above:
    </p>
<pre class="programlisting"><span class="identifier">BOOST_TTI_HAS_FUNCTION_TEMPLATE</span><span class="special">(</span><span class="identifier">AFuncTemplate</span><span class="special">,</span><span class="keyword">char</span><span class="special">,</span><span class="keyword">bool</span><span class="special">,</span><span class="keyword">int</span><span class="special">)</span>
</pre>
<p>
      An alternative form for compilers which do not support variadic macros, and
      which will also work with compilers which do support variadic macros, is to
      specify the template parameters of the instantiation as a single macro argument
      using a Boost PP array:
    </p>
<pre class="programlisting"><span class="identifier">BOOST_TTI_HAS_FUNCTION_TEMPLATE</span><span class="special">(</span><span class="identifier">AFuncTemplate</span><span class="special">,(</span><span class="number">3</span><span class="special">,(</span><span class="keyword">char</span><span class="special">,</span><span class="keyword">bool</span><span class="special">,</span><span class="keyword">int</span><span class="special">)))</span>
</pre>
<p>
      The macro generates a metafunction based on the pattern of "has_function_template_'name_of_inner_function_template'",
      which in our example case would be <code class="computeroutput"><span class="identifier">has_function_template_AFuncTemplate</span></code>.
    </p>
<h4>
<a name="the_type_traits_introspection_library.tti_detail_has_function_template.h1"></a>
      <span class="phrase"><a name="the_type_traits_introspection_library.tti_detail_has_function_template.invoking_the_metafunction"></a></span><a class="link" href="tti_detail_has_function_template.html#the_type_traits_introspection_library.tti_detail_has_function_template.invoking_the_metafunction">Invoking
      the metafunction</a>
    </h4>
<p>
      To use this macro to test whether our function template exists the metafunction
      the macro creates is invoked with the enclosing type, the instantiated return
      type, and the instantiated function parameters, with the resulting <code class="computeroutput"><span class="identifier">value</span></code> being a compile time boolean constant
      which is <code class="computeroutput"><span class="keyword">true</span></code> if the function
      template exists, or <code class="computeroutput"><span class="keyword">false</span></code> otherwise.
      We use each of our needed types as separate parameters, with the function parameters
      being enclosed in an MPL forward sequence. We would have:
    </p>
<pre class="programlisting"><span class="identifier">has_function_template_AFuncTemplate</span><span class="special">&lt;</span><span class="identifier">AType</span><span class="special">,</span><span class="keyword">double</span><span class="special">,</span><span class="identifier">boost</span><span class="special">::</span><span class="identifier">mpl</span><span class="special">::</span><span class="identifier">vector</span><span class="special">&lt;</span><span class="keyword">char</span><span class="special">,</span><span class="keyword">bool</span> <span class="special">*,</span><span class="keyword">int</span> <span class="special">&amp;&gt;</span> <span class="special">&gt;::</span><span class="identifier">value</span>
</pre>
<h4>
<a name="the_type_traits_introspection_library.tti_detail_has_function_template.h2"></a>
      <span class="phrase"><a name="the_type_traits_introspection_library.tti_detail_has_function_template.introspecting_the_other_function_template"></a></span><a class="link" href="tti_detail_has_function_template.html#the_type_traits_introspection_library.tti_detail_has_function_template.introspecting_the_other_function_template">Introspecting
      the other function template</a>
    </h4>
<p>
      If we chose to try to introspect the second <code class="computeroutput"><span class="identifier">AFuncTemplate</span></code>
      within <code class="computeroutput"><span class="identifier">AType</span></code> we might choose
      an instantiation of:
    </p>
<pre class="programlisting"><span class="keyword">int</span> <span class="identifier">AFuncTemplate</span><span class="special">&lt;</span><span class="keyword">long</span><span class="special">,</span><span class="number">7435</span><span class="special">&gt;(</span><span class="keyword">long</span><span class="special">)</span>
</pre>
<p>
      Our generation of the metafunction would then be:
    </p>
<pre class="programlisting"><span class="identifier">BOOST_TTI_HAS_FUNCTION_TEMPLATE</span><span class="special">(</span><span class="identifier">AFuncTemplate</span><span class="special">,</span><span class="keyword">long</span><span class="special">,</span><span class="number">7435</span><span class="special">)</span>
</pre>
<p>
      or
    </p>
<pre class="programlisting"><span class="identifier">BOOST_TTI_HAS_FUNCTION_TEMPLATE</span><span class="special">(</span><span class="identifier">AFuncTemplate</span><span class="special">,(</span><span class="number">2</span><span class="special">,(</span><span class="keyword">long</span><span class="special">,</span><span class="number">7435</span><span class="special">)))</span>
</pre>
<p>
      and our invocation of the metafunction would now be:
    </p>
<pre class="programlisting"><span class="identifier">has_function_template_AFuncTemplate</span><span class="special">&lt;</span><span class="identifier">AType</span><span class="special">,</span><span class="keyword">int</span><span class="special">,</span><span class="identifier">boost</span><span class="special">::</span><span class="identifier">mpl</span><span class="special">::</span><span class="identifier">vector</span><span class="special">&lt;</span><span class="keyword">long</span><span class="special">&gt;</span> <span class="special">&gt;::</span><span class="identifier">value</span>
</pre>
<h4>
<a name="the_type_traits_introspection_library.tti_detail_has_function_template.h3"></a>
      <span class="phrase"><a name="the_type_traits_introspection_library.tti_detail_has_function_template.other_considerations"></a></span><a class="link" href="tti_detail_has_function_template.html#the_type_traits_introspection_library.tti_detail_has_function_template.other_considerations">Other
      considerations</a>
    </h4>
<p>
      In our two examples above we could not introspect both function templates in
      the same namespace using the BOOST_TTI_HAS_FUNCTION_TEMPLATE macro as we would
      be generating two metafunctions with the same name, which would be <code class="computeroutput"><span class="identifier">has_function_template_AFuncTemplate</span></code>, thus
      violating the One Definition Rule. The solution to this is the use of the complex
      macro form.
    </p>
<p>
      The macro for generating the metafunction for introspecting function templates
      also has, like other macros in the TTI library, a complex macro form where
      the end-user can directly specify the name of the metafunction to be generated.
      The corresponding macro is BOOST_TTI_TRAIT_HAS_FUNCTION_TEMPLATE, where the
      first parameter is the name of the metafunction to be generated, the second
      parameter is the member function template name, and the remaining parameters
      are the instantiated template parameters.
    </p>
<p>
      For our first example we could have
    </p>
<pre class="programlisting"><span class="identifier">BOOST_TTI_TRAIT_HAS_FUNCTION_TEMPLATE</span><span class="special">(</span><span class="identifier">FirstMetafunction</span><span class="special">,</span><span class="keyword">char</span><span class="special">,</span><span class="keyword">bool</span><span class="special">,</span><span class="keyword">int</span><span class="special">)</span>
</pre>
<p>
      or for the non-variadic macro form
    </p>
<pre class="programlisting"><span class="identifier">BOOST_TTI_TRAIT_HAS_FUNCTION_TEMPLATE</span><span class="special">(</span><span class="identifier">FirstMetafunction</span><span class="special">,(</span><span class="number">3</span><span class="special">,(</span><span class="keyword">char</span><span class="special">,</span><span class="keyword">bool</span><span class="special">,</span><span class="keyword">int</span><span class="special">)))</span>
</pre>
<p>
      which generates a metafunction whose name would be <code class="computeroutput"><span class="identifier">FirstMetafunction</span></code>.
    </p>
<p>
      For our second example we could have
    </p>
<pre class="programlisting"><span class="identifier">BOOST_TTI_TRAIT_HAS_FUNCTION_TEMPLATE</span><span class="special">(</span><span class="identifier">SecondMetafunction</span><span class="special">,</span><span class="identifier">AFuncTemplate</span><span class="special">,</span><span class="keyword">long</span><span class="special">,</span><span class="number">7435</span><span class="special">)</span>
</pre>
<p>
      or for the non-variadic macro form
    </p>
<pre class="programlisting"><span class="identifier">BOOST_TTI_TRAIT_HAS_FUNCTION_TEMPLATE</span><span class="special">(</span><span class="identifier">SecondMetafunction</span><span class="special">,</span><span class="identifier">AFuncTemplate</span><span class="special">,(</span><span class="number">2</span><span class="special">,(</span><span class="keyword">long</span><span class="special">,</span><span class="number">7435</span><span class="special">)))</span>
</pre>
<p>
      which generates a metafunction whose name would be <code class="computeroutput"><span class="identifier">SecondMetafunction</span></code>.
    </p>
<p>
      In all other respects the resulting metafunctions generated works exactly the
      same as when using the simpler macro form previously illustrated.
    </p>
<p>
      If you do use the simple macro form, which generates the metafunction name
      from the name of the function template you are introspecting, you can use a
      corresponding macro, taking the name of the function template as a single parameter,
      to create the appropriate metafunction name if you do not want to remember
      the pattern for generating the metafunction name. This macro name is <code class="computeroutput"><span class="identifier">BOOST_TTI_HAS_FUNCTION_TEMPLATE_GEN</span></code> as in
    </p>
<pre class="programlisting"><span class="identifier">BOOST_TTI_HAS_FUNCTION_TEMPLATE_GEN</span><span class="special">(</span><span class="identifier">AFuncTemplate</span><span class="special">)</span>
</pre>
<p>
      which would generate the name <code class="computeroutput"><span class="identifier">has_function_template_AFuncTemplate</span></code>.
    </p>
<p>
      When invoking the appropriate metafunction a fourth template argument may optionally
      be given which holds a Boost FunctionTypes tag type. This optional template
      argument is of much less use for function templates than for non-static member
      function templates since static member function templates, like static member
      functions, can not have cv-qualifications. which a number of Boost FunctionTypes
      tags provide. Nonetheless this optional Boost FunctionTypes tag is available
      for end-user use and may come in handy in certain rare cases, as when some
      calling convention qualification for the function template needs to be specified.
      If you do use a Boost FunctionTypes tag type for cv-qualification, such as
      <code class="computeroutput"><span class="identifier">boost</span><span class="special">::</span><span class="identifier">function_types</span><span class="special">::</span><span class="identifier">const_qualified</span></code> to look for a function template
      it will be applied when introspecting for the member function template side
      of the match, but will ensure that introspecting for the static member function
      template side of the match will always fail. In either case no compiler error
      will be generated.
    </p>
</div>
<table xmlns:rev="http://www.cs.rpi.edu/~gregod/boost/tools/doc/revision" width="100%"><tr>
<td align="left"></td>
<td align="right"><div class="copyright-footer">Copyright © 2011-2013 Tropic Software
      East Inc<p>
        Distributed under the Boost Software License, Version 1.0. (See accompanying
        file LICENSE_1_0.txt or copy at <a href="http://www.boost.org/LICENSE_1_0.txt" target="_top">http://www.boost.org/LICENSE_1_0.txt</a>)
      </p>
</div></td>
</tr></table>
<hr>
<div class="spirit-nav">
<a accesskey="p" href="tti_detail_has_function.html"><img src="../../../../../doc/src/images/prev.png" alt="Prev"></a><a accesskey="u" href="../index.html"><img src="../../../../../doc/src/images/up.png" alt="Up"></a><a accesskey="h" href="../index.html"><img src="../../../../../doc/src/images/home.png" alt="Home"></a><a accesskey="n" href="tti_nested_type.html"><img src="../../../../../doc/src/images/next.png" alt="Next"></a>
</div>
</body>
</html>
